#!/bin/bash
############################################################################
# Copyright (C) 2007 Lawrence Livermore National Security, LLC
# Produced at Lawrence Livermore National Laboratory.
# Written by Jim Garlick <garlick@llnl.gov>.
# UCRL-CODE-235119
# 
# This file is part of nfsroot, a network root file system utility.
# 
# nfsroot is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# nfsroot is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with nfsroot; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
############################################################################
#
# rc.nfsroot - nfsroot startup (master script) which will exec /sbin/init
#
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
METHODS="none aufs unionfs bind ram"
configfile=/etc/sysconfig/nfsroot
if [ -f $configfile ]; then
   . $configfile
fi
xprog=rc.nfsroot

panic () {
   echo "${xprog}: $*" >&2
   echo "${xprog}: giving up - please fix the world and reboot me" >&2
   sleep 86400 # one day
   exit 1
}

if ! [ -e /proc/mounts ]; then
    echo "${xprog}: mounting /proc" >&2
    mount -n -t proc /proc /proc
fi

# Since rc.sysinit may mount -oremount,rw later, test that now.
# Unintentional sharing will corrupt root file system.
if ! touch / 2>/dev/null; then
   mount -oremount,rw / 2>/dev/null
   if touch / 2>/dev/null; then
       panic "root file system is mounted ro but exported rw"
   fi
fi

# The successful script will exec init and not return.
# Otherwise, script will return failure (1).
for method in ${METHODS}; do
   . /etc/rc.nfsroot-${method}
done
panic "failed to prepare root file system for use"
